package com.jieshen.lesson3;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class MouseListenerTest {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

class MyFrame extends Frame {

    ArrayList points;
    //监听鼠标当前位置，用集合存储这个点
    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,300);

        points = new ArrayList<>();

        //鼠标监听器，正对这个窗口
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画画，监听鼠标事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    public void addPoint(Point point){
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter {
        //鼠标，按下，弹起，按住不放

        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //点击的时候，在界面上产生一个点
            myFrame.addPoint(new Point(e.getX(),e.getY()));

            //每次点击鼠标都要重画一遍
            myFrame.repaint();//刷新
        }
    }
}

